<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="pg"  uri="http://jsptags.com/tags/navigation/pager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/right.js"></script>
<script type="text/javascript">
//授权
function addOrUpdatePermission(field) {
	//设置DER同步，只在当前页面有效
    javascript:dwr.engine.setAsync(false);
	
	if(field.checked) {//默认继承角色权限和组织权限
		//$(field.getAttribute("moduleId")+"_USE").checked = true;
		<c:if test="${rightType eq 'User' }">
			$(field.getAttribute("moduleId")+"_EXTROLE").checked = true;
			$(field.getAttribute("moduleId")+"_EXTORG").checked = true;
			addOrUpdateRoleExtends(field);
			addOrUpdateOrgExtends(field);
		</c:if>
	}

	right.addOrUpdatePermission (
		"${rightType}",
		${rightOwnerId},
		field.getAttribute("moduleId"),
		field.getAttribute("permission"),
		field.checked
	);
	
	deleteRightRecord(field);
}

//删除权限记录
function deleteRightRecord(field) {
	if(
		$(field.getAttribute("moduleId")+"_C").checked==false &&
		$(field.getAttribute("moduleId")+"_R").checked==false &&
		$(field.getAttribute("moduleId")+"_U").checked==false &&
		$(field.getAttribute("moduleId")+"_D").checked==false &&
		
		$(field.getAttribute("moduleId")+"_ASSIGNUSER").checked==false &&
		$(field.getAttribute("moduleId")+"_ASSIGNROLE").checked==false &&
		$(field.getAttribute("moduleId")+"_ASSIGNORG").checked==false &&
		$(field.getAttribute("moduleId")+"_ASSIGNRIGHT").checked==false &&
		
		$(field.getAttribute("moduleId")+"_IMPORTEXCEL").checked==false &&
		$(field.getAttribute("moduleId")+"_EXPORTEXCEL").checked==false &&
		$(field.getAttribute("moduleId")+"_EXTROLE").checked==false &&
		$(field.getAttribute("moduleId")+"_EXTORG").checked==false) {
		right.deletePermission(
			"${rightType}",
			${rightOwnerId},
			field.getAttribute("moduleId")	
		);
	}
}

//设置角色的继承特性
function addOrUpdateRoleExtends(field){
	right.addOrUpdateRoleInherit(
		${rightOwnerId},
		field.getAttribute("moduleId"),
		field.checked
	);
	deleteRightRecord(field);
}

//设置组织的继承特性
function addOrUpdateOrgExtends(field){
	right.addOrUpdateOrgInherit(
		${rightOwnerId},
		field.getAttribute("moduleId"),
		field.checked
	);
	deleteRightRecord(field);
}

//点击启用checkbox
function usePermission(field){
	//设置为同步方式，以便DWR依次发出下列请求
	javascript:dwr.engine.setAsync(false);
	
	if(field.checked){
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_C"));
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_R"));
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_U"));
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_D"));
		
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_ASSIGNUSER"));
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_ASSIGNROLE"));
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_ASSIGNORG"));
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_ASSIGNRIGHT"));
		
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_IMPORTEXCEL"));
		addOrUpdatePermission($(field.getAttribute("moduleId")+"_EXPORTEXCEL"));
		<c:if test="${rightType eq 'User' }">
			addOrUpdateRoleExtends($(field.getAttribute("moduleId")+"_EXTROLE"));
			addOrUpdateOrgExtends($(field.getAttribute("moduleId")+"_EXTORG"));
		</c:if>
	}else{
		right.deletePermission(
			"${rightType}",
			${rightOwnerId},
			field.getAttribute("moduleId")	
		);
		$(field.getAttribute("moduleId")+"_C").checked = false;
		$(field.getAttribute("moduleId")+"_R").checked = false;
		$(field.getAttribute("moduleId")+"_U").checked = false;
		$(field.getAttribute("moduleId")+"_D").checked = false;
		
		$(field.getAttribute("moduleId")+"_ASSIGNUSER").checked = false;
		$(field.getAttribute("moduleId")+"_ASSIGNROLE").checked = false;
		$(field.getAttribute("moduleId")+"_ASSIGNORG").checked = false;
		$(field.getAttribute("moduleId")+"_ASSIGNRIGHT").checked = false;
		
		$(field.getAttribute("moduleId")+"_IMPORTEXCEL").checked = false;
		$(field.getAttribute("moduleId")+"_EXPORTEXCEL").checked = false;
		<c:if test="${rightType eq 'User' }">
			$(field.getAttribute("moduleId")+"_EXTROLE").checked = false;
			$(field.getAttribute("moduleId")+"_EXTORG").checked = false;
		</c:if>
	}
}

function initTable() {
	right.getAllPermissionRecord(
		"${rightType}",
		${rightOwnerId},
		function(datas){
			for(var i=0; i < datas.length; i++){
				var moduleId = datas[i][0];
				var cState = datas[i][1];
				var rState = datas[i][2];
				var uState = datas[i][3];
				var dState = datas[i][4];
				
				var userState = datas[i][5];
				var roleState = datas[i][6];
				var orgState = datas[i][7];
				var rightState = datas[i][8];
				
				var iState = datas[i][9];
				var eState = datas[i][10];
				var extRole = datas[i][11];
				var extOrg = datas[i][12];
				
				$(moduleId+"_C").checked = cState == 0 ? false : true;
				$(moduleId+"_R").checked = rState == 0 ? false : true;
				$(moduleId+"_U").checked = uState == 0 ? false : true;
				$(moduleId+"_D").checked = dState == 0 ? false : true;
				
				$(moduleId+"_ASSIGNUSER").checked = userState == 0 ? false : true;
				$(moduleId+"_ASSIGNROLE").checked = roleState == 0 ? false : true;
				$(moduleId+"_ASSIGNORG").checked = orgState == 0 ? false : true;
				$(moduleId+"_ASSIGNRIGHT").checked = rightState == 0 ? false : true;
				
				$(moduleId+"_IMPORTEXCEL").checked = iState == 0 ? false : true;
				$(moduleId+"_EXPORTEXCEL").checked = eState == 0 ? false : true;
				<c:if test="${rightType eq 'User' }">
					$(moduleId+"_EXTROLE").checked = extRole == 0 ? false : true;
					$(moduleId+"_EXTORG").checked = extOrg == 0 ? false : true;
				</c:if>
				//$(moduleId+"_USE").checked = true;
			}
		}
	);
}

</script>

<c:set var="title" value="请给${rightTypeName}【${rightOwnerName}】授权"/>

<title>${title }</title>
</head>
<body onload="initTable()">
<center>
<TABLE class="tableEdit" border="0" cellspacing="1" cellpadding="0" style="width:100%;">
	<TBODY>
		<TR>
			<!-- 这里是添加、编辑界面的标题 -->
			<td align="center" class="tdEditTitle">${title }</TD>
		</TR>
		<TR>
			<td>
			<!-- 主输入域开始 -->

<table class="tableEdit" style="width:100%;" cellspacing="0" border="0" cellpadding="0">
	
	<tr>
		<td  align="center" style="color:blue;font-size:14px;">模块名称</td>	
		<td  align="center" style="color:blue;font-size:14px;">权限</td>	
		<c:if test="${rightType eq 'User' }">
			<td align="center" style="color:blue;font-size:14px;">继承角色权限</td>	
			<td align="center" style="color:blue;font-size:14px;">继承组织权限</td>	
		</c:if>
		<!-- 
		<td align="center" style="color:blue;font-size:14px;">启用</td>	
		 -->
	</tr>
	
	<!-- 输出模块 -->
	<c:forEach items="${pm.resultList }" var="module">
	<tr>
		<td align="center">${module.name }</td>
		<td align="center">
		<input type="checkbox" id="${module.id }_C" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="0">创建
		<input type="checkbox" id="${module.id }_R" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="1">查看
		<input type="checkbox" id="${module.id }_U" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="2">编辑
		<input type="checkbox" id="${module.id }_D" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="3">删除
		
		<input type="checkbox" id="${module.id }_ASSIGNUSER" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="4">分配用户
		<input type="checkbox" id="${module.id }_ASSIGNROLE" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="5">分配角色
		<input type="checkbox" id="${module.id }_ASSIGNORG" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="6">分配组织
		<input type="checkbox" id="${module.id }_ASSIGNRIGHT" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="7">授权
		
		<input type="checkbox" id="${module.id }_IMPORTEXCEL" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="8">导入
		<input type="checkbox" id="${module.id }_EXPORTEXCEL" onclick="addOrUpdatePermission(this)" moduleId="${module.id }" permission="9">导出
		</td>
	<c:if test="${rightType eq 'User' }">
		<td align="center"><input type="checkbox" id="${module.id }_EXTROLE"  onclick="addOrUpdateRoleExtends(this)" moduleId="${module.id }"></td>
		<td align="center"><input type="checkbox" id="${module.id }_EXTORG"  onclick="addOrUpdateOrgExtends(this)" moduleId="${module.id }"></td>
	</c:if>
	<!-- 
		<td align="center"><input type="checkbox" id="${module.id }_USE" onclick="usePermission(this)" moduleId="${module.id }"></td>
	 -->
	</tr>
	
	</c:forEach>
</table>

			<!-- 主输入域结束 -->
			</td>
		</TR>
	</TBODY>
</TABLE>

<TABLE>
		<TR align="center">
			<TD colspan="3" bgcolor="#EFF3F7">
			<input type="button" class="MyButton"
				value="关闭窗口" onclick="window.close()">
			</TD>
		</TR>
</TABLE>
</center>
</body>
</html>